package com.example.demo.action.client;/**
 * Created by kete on 2018-12-05.
 */

import com.example.demo.action.common.CustomProtocol;
import com.example.demo.action.common.MsgModel;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.cglib.core.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 作者：kete
 * 创建时间：2018-12-05 15:07
 * 功能描述：
 * 版本：
 */
@Controller
@RequestMapping("/")
public class IndexController {

    @Autowired
    private CounterService counterService;

    private HeartBeatClient client;

    @ApiOperation("客户端发送消息")
    @RequestMapping(value = "/sendMsg",method = RequestMethod.POST)
    @ResponseBody
    public Map sendMsg(MsgModel model){

        // 利用 actuator 来自增
        counterService.increment("counter.client.push.count");
        Map result = new HashMap();

        client.sendMsg(new CustomProtocol(model.getMsgId(),model.getMsg()));

        result.put("msg",model.getMsg());
        result.put("code","0");

        return result;
    }
}
